package com.example.leetcode.solution;

import com.example.leetcode.ListNode;

public class Solution328 {

    public static void main(String[] args) {
        ListNode head = new ListNode(1,new ListNode(6,new ListNode(2,new ListNode(3,new ListNode(4,new ListNode(5, new ListNode(6)))))));;
        new Solution328().oddEvenList(head);
    }

    public ListNode oddEvenList(ListNode head) {
        if(head == null){
            return null;
        }
        int index = 1;
        ListNode odd = null;
        ListNode oddTemp = null;
        ListNode even = null;
        ListNode evenTemp = null;
        while(head != null){
            if(index % 2 == 0){
                if(even == null){
                    even = new ListNode(head.val);
                    evenTemp = even;
                }else{
                    ListNode temp = evenTemp;
                    evenTemp = new ListNode(head.val);
                    temp.next = evenTemp;
                }
            }else{
                if(odd == null){
                    odd = new ListNode(head.val);
                    oddTemp = odd;
                }else{
                    ListNode temp = oddTemp;
                    oddTemp = new ListNode(head.val);
                    temp.next = oddTemp;
                }
            }
            index++;
            head = head.next;
        }
       // evenTemp.next = null;
        oddTemp.next = even;
        return odd;
    }
}
